<?php
declare(strict_types=1);
namespace App;
// hook controller_use.php

use Field\QuestionThreadConst;
use Field\StoreProductConst;
use Field\SysArticleConst;
use Field\SysPagesConst;
use Field\SysUserColumns;
use Field\SysUserConst;
use Framework\Support\Request;
use Swoole\Http\Response;

class Controller extends \Framework\Libs\Controller
{
    // hook controller_public_start.php

    public array $public_columns=[
        ['prop'=>'adPv','label'=>'展现量','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>0],'width'=>60],
        ['prop'=>'click','label'=>'点击量','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>0],'width'=>60],
        ['prop'=>'charge','label'=>'花费','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>2],'width'=>60],
        ['prop'=>'ctr','label'=>'点击率%','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>4],'width'=>60],
        ['prop'=>'ecpc','label'=>'平均点击花费','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>2],'width'=>60],
        ['prop'=>'ecpm','label'=>'千次展现花费','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>2],'width'=>60],
        ['prop'=>'cart_fee','label'=>'直接加购成本','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>2],'width'=>60],
        ['prop'=>'alipayDirAmt','label'=>'直接成交金额','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>2],'width'=>60],
        ['prop'=>'alipayInshopAmt','label'=>'总成交金额','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>2],'width'=>60],
        ['prop'=>'alipayInshopNum','label'=>'总成交笔数','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>0],'width'=>60],
        ['prop'=>'alipayDirNum','label'=>'直接成交笔数','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>0],'width'=>60],
        ['prop'=>'gmvInshopNum','label'=>'拍下订单笔数','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>0],'width'=>60],
        ['prop'=>'gmvInshopAmt','label'=>'拍下订单金额','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>2],'width'=>60],
        ['prop'=>'prepayInshopAmt','label'=>'总预售成交金额','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>2],'width'=>60],
        ['prop'=>'prepayInshopNum','label'=>'总预售成交笔数','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>0],'width'=>60],
        ['prop'=>'prepayDirAmt','label'=>'直接预售成交金额','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>2],'width'=>60],
        ['prop'=>'prepayDirNum','label'=>'直接预售成交笔数','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>0],'width'=>60],
        ['prop'=>'prepayIndirAmt','label'=>'间接预售成交金额','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>2],'width'=>60],
        ['prop'=>'prepayIndirNum','label'=>'间接预售成交笔数','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>0],'width'=>60],
        ['prop'=>'alipayIndirNum','label'=>'间接成交笔数','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>0],'width'=>60],
        ['prop'=>'alipayIndirAmt','label'=>'间接成交金额','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>2],'width'=>60],
        ['prop'=>'cvr','label'=>'点击转化率%','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>4],'width'=>60],
        ['prop'=>'roi','label'=>'投入产出比','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>2],'width'=>60],
        ['prop'=>'alipayInshopCost','label'=>'总成交成本','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>2],'width'=>60],
        ['prop'=>'cartInshopNum','label'=>'总购物车数','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>0],'width'=>60],
        ['prop'=>'cartDirNum','label'=>'直接购物车数','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>0],'width'=>60],
        ['prop'=>'cartIndirNum','label'=>'间接购物车数','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>0],'width'=>60],
        ['prop'=>'cartRate','label'=>'加购率%','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>4],'width'=>60],
        ['prop'=>'itemColInshopNum','label'=>'收藏宝贝数','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>0],'width'=>60],
        ['prop'=>'shopColDirNum','label'=>'收藏店铺数','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>0],'width'=>60],
        ['prop'=>'shopColInshopCost','label'=>'店铺收藏成本','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>2],'width'=>60],
        ['prop'=>'colCartNum','label'=>'总收藏加购数','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>0],'width'=>60],
        ['prop'=>'colCartCost','label'=>'总收藏加购成本','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>2],'width'=>60],
        ['prop'=>'itemColCart','label'=>'宝贝收藏加购数','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>0],'width'=>60],
        ['prop'=>'itemColCartCost','label'=>'宝贝收藏加购成本','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>2],'width'=>60],
        ['prop'=>'colNum','label'=>'总收藏数','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>0],'width'=>60],
        ['prop'=>'itemColInshopCost','label'=>'宝贝收藏成本','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>2],'width'=>60],
        ['prop'=>'itemColInshopRate','label'=>'宝贝收藏率%','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>4],'width'=>60],
        ['prop'=>'cartCost','label'=>'加购成本','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>2],'width'=>60],
        ['prop'=>'couponShopNum','label'=>'优惠券领取量','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>0],'width'=>60],
        ['prop'=>'shoppingNum','label'=>'购物金充值笔数','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>0],'width'=>60],
        ['prop'=>'shoppingAmt','label'=>'购物金充值金额','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>2],'width'=>60],
        ['prop'=>'wwNum','label'=>'旺旺咨询量','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>0],'width'=>60],
        ['prop'=>'adUv','label'=>'触达人数总量','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>0],'width'=>60],
        ['prop'=>'adUvCost','label'=>'平均触达成本','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>2],'width'=>60],
        ['prop'=>'shopVisitUv','label'=>'访问新客增量','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>0],'width'=>60],
        ['prop'=>'shopVisitUvCost','label'=>'访问新客成本','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>2],'width'=>60],
        ['prop'=>'shopVisitUvRate','label'=>'访问新客流转率%','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>4],'width'=>60],
        ['prop'=>'addNewUv','label'=>'兴趣新客增量','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>0],'width'=>60],
        ['prop'=>'addNewUvAllCost','label'=>'兴趣新客成本','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>2],'width'=>60],
        ['prop'=>'addNewUvUvRate','label'=>'兴趣新客流转率%','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>4],'width'=>60],
        ['prop'=>'firstPurchaseUv','label'=>'首购人群增量','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>0],'width'=>60],
        ['prop'=>'firstPurchaseUvCost','label'=>'首购人群成本','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>2],'width'=>60],
        ['prop'=>'firstPurchaseUvRate','label'=>'首购人群流转率%','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>4],'width'=>60],
        ['prop'=>'repurchaseUvCost','label'=>'复购人群成本','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>2],'width'=>60],
        ['prop'=>'repurchaseUvRate','label'=>'复购人群流转率%','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>4],'width'=>60],
        ['prop'=>'fav_rate','label'=>'收藏率','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>4],'width'=>60],
        ['prop'=>'fav_fee','label'=>'收藏成本','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>2],'width'=>60],
        ['prop'=>'colCartRate','label'=>'收藏加购率%','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>2],'width'=>60],
        ['prop'=>'transaction_cost','label'=>'盈利亏损','hidden'=>0,'sortable'=>1,'cellRenderer'=>'IntRenderer','context'=>['format'=>2],'width'=>60]
    ];

    public string $http_host = '';
    public string $upload_domain;
    public array $index_search = [];
    public array $role;
    public array $role_kv;
    public bool $is_auth = false;
    public SysUserColumns $token;
    public string $login_url = "";
    public string $index_dir = '';
    // hook controller_public_end.php
    public function __construct()
    {
        parent::__construct();
        $this->role_kv = $this->SysRole->data_kv;
        $this->role = $this->SysRole->data_array;

        // hook controller_construct.php
    }

    public function init(string $controller,array $route,string $action, Request $request, Response $response): void
    {
        parent::init($controller, $route, $action, $request, $response); // TODO: Change the autogenerated stub

        $this->token = new SysUserColumns();
        $this->token->UserID=0;
        $this->token->RoleID=0;
        $this->token->NickName="游客";
        $this->token->UserLevelID=0;
        $this->index_url = '../../' . implode('/', $route);
        $this->login_url = '../../';
        $this->assign['http_host']=$this->http_host;

    }
    // hook controller_start.php


    //仅获取登录信息
    public function auth(): void
    {
        // hook controller_auth_start.php

        if(!$this->is_auth){
            $this->is_auth = true;
            $this->token = $this->UserInfo();
        }
        empty($this->token->UserID) && $this->token->UserID = 0;
        if (_CONF('site_closed') && !$this->not_closed) {
            if (empty($this->token->RoleID) || $this->token->RoleID != 1) {
                $this->response('9999', [], '站点升级中,暂停访问,请稍后再试...');
            }
        }
        // hook controller_auth_end.php
        parent::auth();

    }

    //需要拦截登录
    public function login()
    {
        // hook controller_login_start.php
        $this->auth();
        empty($this->token->RoleID) && $this->token->RoleID = 0;
        if (empty($this->token->UserID)) {//需要登录的弹出未登录
            $this->response('0401', [], '请先登录', trim($this->login_url), 302);
        }
        if ($this->token->RoleID != 1) {//非超管--实现登录拦截
            $route = implode('_', $this->route);
            $k = strtolower($route);
            $exist = !empty($this->token->RoleID) ? $this->_T('Auth')->exist($this->token->RoleID . ',' . $k) : false;//判断用户组是否有权限
            if (!$exist) {
                $k = strtolower($route . '_' . $this->_method);
                $exist = $this->token->RoleID ? $this->_T('Auth')->exist($this->token->RoleID . ',' . $k) : false;//判断用户组是否有权限
            }
            if (!$exist) {
                $this->response('0402', [], '未授权访问', trim($this->login_url), 302);
            }
        }
        // hook controller_login_end.php
        parent::login(); // TODO: Change the autogenerated stub
    }

    public function UserInfo():SysUserColumns
    {
        // hook controller_userfnfo_start.php
        empty($name) && $name = _CONF('session_id');
        empty($name) and $name = 'session_id';

        $this->session_id = $this->request->cookie(_CONF('cookie_prefixes') . $name);

        // hook controller_userfnfo_middle.php
        return $this->SysUser->check_token($this->request, $this->session_id);
        // hook controller_userfnfo_end.php
    }

    public function Dir(string $_tpl):string
    {
        $route = $this->route;
        $count = count($route);
        $tpl = $route[$count - 2] . '.' . $route[$count - 1];
        unset($route[$count - 2], $route[$count - 1]);
        if ($_tpl) {
            $l = ltrim($_tpl, '@');
            if ($l != $_tpl) {
                $_tpl = implode('/', $route) . '/View/diy/' . $l;
            }
        } else {
            $_tpl = implode('/', $route) . '/View/diy/' . $tpl;
        }
        return !empty($_ENV['plugin_view_files'][$_tpl . '.html']) ? $_ENV['plugin_view_files'][$_tpl . '.html'] : __APPDIR__ . 'Controller/' . $_tpl . '.html';
    }

    public function GetPatchByID(int $AttachID)
    {

       return $this->SysAlbumAttach->GetPatchByID($AttachID);
    }
    public function GetUrl(array $data)
    {
        $url=[];
        $Cate = $this->SysCate->DataKey[$data['CateID']];
        $url[]=$Cate['Url'];
        if(isset($data['ArticleID'])){
            $url[]=$data['ArticleID'].'.html';
        }elseif(isset($data['PageID'])){
            $url[]=$data['PageID'].'.html';
        }elseif(isset($data['ProductID'])){
            $url[]=$data['ProductID'].'.html';
        }elseif(isset($data['ThreadID'])){
            $url[]=$data['ThreadID'].'.html';
        }
        return implode('/', $url);
    }

    /**
     * @param string $tpl
     * @param array $_data
     *
     * @return string
     * @throws \Exception
     * 加载模板文件输出PHP执行结果
     */
    public function View(array $_data = [],string $_tpl = '')
    {

        // hook controller_view_start.php
        $route = $this->route;

        $__PRE__ = implode('_', $route) . '_';
        $__DIR__ = $route[0];

        $count = count($route);
        $tpl = $route[$count - 1] . '.' . $this->action;
        unset($route[$count -1]);
        if ($_tpl) {
            $l = ltrim($_tpl, '@');
            if ($l != $_tpl) {
                $_tpl = implode('/', $route) . '/View/' . $l;
            }
        } else {
            $_tpl = implode('/', $route) . '/View/' . $tpl;
        }

        $this->index_dir = $__DIR__;
        $this->index_table = 'T_' . get_uniqid(16);
        $this->index_url = str_replace('$__DIR__', $this->index_dir, $this->index_url);
        $_temp_name = !empty($_ENV['plugin_view_files'][$_tpl . '.html']) ? $_ENV['plugin_view_files'][$_tpl . '.html'] : __APPDIR__ . 'Controller/' . $_tpl . '.html';
        // hook controller_view_filename.php
        //var_dump($_temp_name);
        !is_file($_temp_name) and $this->response('0007', [], '模板不存在:' . $_tpl);

        if ($this->assign) {
            extract($this->assign, EXTR_OVERWRITE);
        }
        if (is_array($_data)) {
            extract($_data, EXTR_OVERWRITE);
        }
        !empty($title) && $title[]=$_ENV['conf']['Title'];
        $_in_view = true;
        ob_start();
        include _include($_temp_name);
        $data = ob_get_contents();
        ob_end_clean();
        // hook controller_view_end.php
        return $data;
    }



    //获取某个月的每一天
    public function get_day_by_ym($ym): array
    {
        $t = date("t", strtotime($ym . '01000000'));
        $ymd = [];
        for ($i = 1; $i <= $t; $i++) {
            $ymd[] = $i < 10 ? $ym . '0' . $i : $ym . $i;
        }
        return $ymd;
    }

    public function CondTimeRange($time, $sec = 1): array
    {
        list($start, $end) = explode(' - ', $time);
        if ($sec) {
            $s = explode(' ', $start);
            $e = explode(' ', $end);
            empty($s[1]) && $start .= ' 00:00:00';
            empty($e[1]) && $end .= ' 23:59:59';
            return ['>=' => strtotime($start), '<=' => strtotime($end)];
        }
        return ['>=' => $start, '<=' => $end];
    }

    public function TimeRange($time, $sec = 1): array
    {
        list($start, $end) = explode(' - ', $time);

        if ($sec == 1) {
            $s = explode(' ', $start);
            $e = explode(' ', $end);
            empty($s[1]) && $start .= ' 00:00:00';
            empty($e[1]) && $end .= ' 23:59:59';
        }
        $start = strtotime($start);
        $end = strtotime($end) + 86400;
        $data = [];
        for ($i = $start; $i < $end; $i += 86400) {
            $data[] = date('Ymd', $i);
        }

        return $data;
    }

    public function DestroyToken($name = ''): void
    {
        // hook controller_destroytoken_start.php
        empty($name) && $name = _CONF('session_id');
        $this->setcookie($name, '', -1000);
        // hook controller_destroytoken_end.php
    }
    public function getImg($key): string
    {
        return $this->upload_domain . ($key ? $key : '/images/default_avatar_male.gif');
    }

    /*
    public function SHA1withRSA($data): string
    {
        $key = _SYS('Rsa');
        openssl_sign($data, $signature, $key['touni']);
        return base64_encode($signature);
    }

    public function verifySign($data, $sign): bool
    {
        $key = _SYS('Rsa');
        if (openssl_verify($data, base64_decode($sign), $key['fromuni'])) {
            return true;
        } else {
            return false;
        }
    }

    //A-Z 组合
    public function mkaz($num): false|array
    {
        if ($num > 702) {
            return false;
        }
        $word = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $arr = [];
        for ($i = 0; $i <= $num; $i++) {
            $v = floor($i / 26);
            $arr[] = ($v ? $word[$v - 1] : '') . $word[$i % 26];
        }
        return $arr;
    }

    public function checkaccess($cate, $allow = 'allowread'): bool
    {
        $level_id = !empty($this->token['level_id']) ? $this->token['level_id'] : 0;
        $gid = !empty($this->token[SysUserConst::RoleID]) ? $this->token[SysUserConst::RoleID] : 0;
        if ($cate['status'] != 1) {
            return false;
        } else if ($gid == 1 || $gid == 2 || empty($_cate['accesson'])) {
            return true;
        } else {
            if (!empty($cate['access']) && empty($access[$level_id][$allow])) {
                return true;
            }
        }
        return false;
    }

    //A-Z 最大列数量
    public function rtaz($az): float|false|int
    {
        if (isset($az[2])) {
            return false;
        }
        $word = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        if (isset($az[1])) {
            $i = (strpos($word, $az[0]) * 26) + strpos($word, $az[1]) + 26;
        } else {
            $i = strpos($word, $az[0]);
        }
        return $i + 1;
    }

    public function base32_decode($str): string
    {
        $alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
        $str = strtoupper($str);
        $decoded = '';

        $padding = (8 - strlen($str) % 8) % 8;
        $str = str_pad($str, strlen($str) + $padding, '=', STR_PAD_RIGHT);

        $position = 0;
        $accum = 0;
        $bits = 0;
        $length = strlen($str);
        while ($position < $length) {
            $current = strpos($alphabet, $str[$position]);
            $accum = ($accum << 5) | $current;
            $bits += 5;

            if ($bits >= 8) {
                $decoded .= chr(($accum >> ($bits - 8)) & 0xFF);
                $bits -= 8;
            }
            $position++;
        }
        return $decoded;
    }

    public function TOTPMake($t = 0,$k=""): string
    {
        if(empty($t)){
            $t =  time();
            $k = xn_rand(20).xn_encrypt($t).xn_rand(20);
            $this->_response->cookie('_t', $k,time() +60,'/',"","",true);
            $key = $this->base32_decode($k);
        }else{
            $key = $this->base32_decode($k);
        }

        $time = pack('J', $t);

        $hash = hash_hmac('sha1', $time, $key, true);
        $offset = ord($hash[19]) & 0xf;
        $otp = (
                ((ord($hash[$offset + 0]) & 0x7f) << 24) |
                ((ord($hash[$offset + 1]) & 0xff) << 16) |
                ((ord($hash[$offset + 2]) & 0xff) << 8) |
                (ord($hash[$offset + 3]) & 0xff)
            ) % 10000;
        return str_pad((string)$otp, 4, '0', STR_PAD_LEFT);
    }

    public function TOTPCheck($key): bool
    {
        $code =  $this->request->cookie('_t');
        $time =intval(xn_decrypt(substr($code,20,-20)));
        if(empty($time)||empty($key)){
            return false;
        }
        if($time+60<time()){
            return false;
        }
        $calculatedOTP = $this->TOTPMake($time,$code);
        $this->set_cookie('_t', "",-10000);
        return $key===$calculatedOTP;
    }
    */





    // hook controller_end.php
}

/**
* 获取取查询条件
 * @return array
 * /
 * public function index_search_fmt(): array
 * {
 * $cond = [];
 * foreach ($this->index_search as $v) {
 *
 * !isset($v['def']) && $v['def'] = '';
 * $name = $this->request->get($v['name'], $v['def']);
 * (empty($name) && !empty($v['def'])) && $name = $v['def'];
 *
 * if (empty($v['radio']) && !empty($name)) {
 * $name = explode(',', $name);
 * }
 * !isset($v['cond']) && $v['cond'] = '';
 * if (!empty($v['cond'])) {
 * if (is_array($v['cond'])) {
 * if (isset($v['cond']['OR']['string'])) {
 * if ($name) {
 * foreach ($v['cond']['OR']['string'] as $r) {
 * $cond['OR'][$r]['LIKE'] = trim($name);
 * }
 * if (is_numeric($name)) {
 * foreach ($v['cond']['OR']['int'] as $r) {
 * $cond['OR'][$r]['LIKE'] = $name;
 * }
 * }
 * }
 * } elseif (isset($v['cond']['OR'])) {
 * foreach ($v['cond']['OR'] as $r) {
 * $name && $cond['OR'][$r]['LIKE'] = trim($name);
 * }
 * } else {
 * foreach ($v['cond'] as $r) {
 * $name && $cond[$r]['LIKE'] = trim($name);
 * }
 * }
 * } elseif ($v['type'] == 'time') {
 * $name && $cond[$v['name']] = $this->CondTimeRange($name, !empty($v['range']));
 * } else {
 * $name && $cond[$v['name']]['LIKE'] = trim($name);
 * }
 * } else {
 * if (is_numeric($v['def']) && $v['def'] < 0) {
 * $name > 0 && $cond[$v['name']] = $name + $v['def'];
 * } else {
 * $name && $cond[$v['name']] = $name;
 * }
 * }
 * }
 * return $cond;
 * }
 * public function getCond()
 * {
 * $startRow = $this->request->param('startRow',0);
 * $endRow = $this->request->param('endRow',0);
 * $sort=[];
 * $cond = [];
 * if($endRow){//ag表格模式
 * $limit = $endRow-$startRow;
        * $page = $endRow/$limit;
        * $sortModel =  $this->request->param('sortModel',[]);
        * $filterModel =  $this->request->param('filterModel',[]);
 *
* foreach ($sortModel as $row){
            * $sort[$row['colId']]=$row['sort']=='asc'?1:-1;
        * }
        * if(!empty($filterModel['filterType']) && $filterModel['filterType']=='join'){
            * if(!empty($filterModel['conditions'])){
                * $cd = [];
                * foreach ($filterModel['conditions'] as $condition){
                    * if( $filterModel['type']=='OR'){
                        * $cd[$condition['colId']]["OR"][]= $this->makeCond($condition);
                    * }else{
                        * $cd[$condition['colId']][]= $this->makeCond($condition);
                    * }
                * }
                * if(!empty($cd)){
                    * if( $filterModel['type']=='OR'){
                        * $cond["OR"]=$cd;
                    * }else{
                        * $cond=$cd;
                    * }
                * }
            * }
        * }else if(!empty($filterModel) && !empty($filterModel['colId']) ) {
            * $cond[$filterModel['colId']] = $this->makeCond($filterModel);
        * }else if(!empty($filterModel)){
            * foreach ($filterModel as $k => $row){
                * if(!empty($row['conditions'])){
                    * $cd = [];
                    * foreach ($row['conditions'] as $condition){
                        * $cd[$k][]= $this->makeCond($condition);
                    * }
                    * if($row['operator']=='OR'){
                        * $cond["OR"][]=$cd;
                    * }else{
                        * $cond[]=$cd;
                    * }
                * }else{
                    * $cond[$k] = $this->makeCond($row);
                * }
            * }
        * }
    * }else{//兼容老模式
        * $limit = $this->request->param('limit',10);
        * $page = $this->request->param('page',1);
        * $field = $this->request->get('field', '');
        * $order = $this->request->get('order', '');
        * $cond = $this->index_search_fmt();
        * if($field){
            * $sort[$field]=$order == 'asc' ? 1 : -1;
        * }
    * }
    * return ['page'=>$page,'limit'=>$limit, 'cond'=>$cond,'rank'=>$sort];
* }
 *
* public function makeCond($data)
* {   $cond = [];
* if(empty($data)){
* return '';
* }
* switch ($data['type']){
* case 'contains'://like
* $cond['LIKE']=$data['filter'];
* break;
* case 'startsWith'://like
* $cond['RLIKE']=$data['filter'];
* break;
* case 'endsWith'://like
* $cond['LLIKE']=$data['filter'];
* break;
* case 'notContains'://not like
* $cond['NLIKE']=$data['filter'];
* break;
* case 'equals'://=
* $cond['=']=$data['filter'];
* break;
* case 'notEqual'://=
* $cond["!="]=$data['filter'];
* break;
* case 'blank'://=
* $cond['NULL']='NULL';
* break;
* case 'notBlank'://=
* $cond['NNULL']='NULL';
* break;
* case 'greaterThan':// >
* $cond[">"]=$data['filter'];
* break;
* case 'greaterThanOrEqual':// >=
* $cond[">="]=$data['filter'];
* break;
* case 'lessThan':// <
* $cond["<"]=$data['filter'];
* break;
* case 'lessThanOrEqual':// <=
* $cond["<="]=$data['filter'];
* break;
* case 'inRange':// ><
* $cond["><"]=[$data['filter'],$data['filterTo']];
* break;
* }
* return $cond;
* }
**/
?>